<?php
/**
 * 产品公共模型
 * @author shipanpan
 *
 */
class Application_Model_Product extends Model_Base {
	//产品状态
	const STATUS = [1 => '正常', 2 => '断货', 3 => '停产'];

	//产品审核通过状态类别
	const CHECK_SUCCESS_STATUS = [4 => "审核通过", 8 => "疑似侵权", 10 => "暂不上架"];
	//产品审核不通过状态类别
	const CHECK_FAIL_STATUS = [5 => "产品信息审核不通过", 6 => "产品性质审核不通过", 7 => " 产品开发重复"];

	//产品货源状态
	const RESOURCE_NORMAL_STATUS = 1;
	const RESOURCE_STOCKOUT_STATUS = 2;
	const RESOURCE_STOP_PRODUCT_STATUS = 3;
	const RESOURCE_STATUS = [self::RESOURCE_NORMAL_STATUS => '正常', self::RESOURCE_STOCKOUT_STATUS => '断货', self::RESOURCE_STOP_PRODUCT_STATUS => '停产'];

	/**
	 * 获取产品状态名称
	 * @param  [type] $statusNumber 产品状态数值;为null时返回所有的产品状态
	 * @return string|array
	 */
	public static function getStatusName($statusNumber = null) {
		if (is_null($statusNumber)) {
			return self::STATUS;
		}

		return self::STATUS[$statusNumber] ?? "";
	}

	/**
	 * 获取产品审核状态名称
	 * @param  [type] $CheckStatusNumber 审核状态数值，为null时，返回所有的审核状态
	 * @return string|array
	 */
	public static function getCheckStatusName($CheckStatusNumber = null) {
		$allCheckStatus = [3 => "待审核"] + self::CHECK_SUCCESS_STATUS + self::CHECK_FAIL_STATUS;
		if (is_null($CheckStatusNumber)) {
			return $allCheckStatus;
		}

		return $allCheckStatus[$CheckStatusNumber] ?? "";
	}

	/**
	 * 传输SKU的父ID取它自己
	 * @param 父ID
	 * @param 字段
	 * @param 商品状态
	 * @return Array
	 */
	public function getProducts($pid = 0, $fields = ['id'], $product_status = 1) {

		$where = ' WHERE product_status = ' . $product_status;
		$pid !== 0 && $where .= ' AND id = ' . $pid;
		return Model_Base::db()->fetchAll('SELECT ' . implode(',', $fields) . ' FROM p_product');
	}

	/**
	 * 通过商品SKU取其相关信息
	 * @param SKU的编号
	 * @param 字段
	 * @return Array
	 */
	public function getProductsByGoodSN($goods_sn = [], $fields = '*') {

		if (count($goods_sn)) {
			$where = ' WHERE goods_sn IN("' . implode('","', $goods_sn) . '")';
		} else {
			$where = '';
		}

		$sql = 'SELECT ' . $fields . ' FROM p_product' . $where;
		return $this->db()->fetchAssoc($sql);
	}

	public function fetchCateIdsByCateId($cid){

		return $this -> db() -> fetchCol('SELECT id FROM p_product_catalog WHERE path like "%' . $cid . '%"');
	}

	public function getProduct($goods_sn, $fields = '*') {

		return $this->db()->fetchAll('SELECT p.' . $fields . ',pp.goods_resource_status FROM p_product p left join p_product_purchase pp using(goods_sn) WHERE goods_sn = "' . $goods_sn . '"');
	}
}